package customControls;

import android.annotation.SuppressLint;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.example.jaden.washer.R;

import java.util.List;

import dormitory.Dormitory;

public class WashPatternAdapter extends ArrayAdapter {
    Pattern pattern;
    int Position;
    private int resourceId;
    ;
    public  WashPatternAdapter (@NonNull Context context, int resource, List<Pattern> objects) {
        super(context, resource, objects);
        resourceId = resource;
    }
    public class ViewHolder
    {
        TextView washpattern;
        TextView price;
        TextView time;
    }
    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {

        Position=position;
        //根据position 得到当前子项的Commodity实例
        pattern= (Pattern) getItem(position);
        ViewHolder viewHolder = null;
        if(convertView==null)
        {
            //Log.d(TAG, "getView: "+position);
            //为当前子项加载指定布局->resourceId
            convertView= LayoutInflater.from(getContext()).inflate(resourceId,parent,false);
            viewHolder=new ViewHolder();
            //获取控件实例
            getInstance(viewHolder,convertView);
            convertView.setTag(viewHolder);
        }
        else {
            viewHolder= (ViewHolder) convertView.getTag();
        }
        //显示控件信息
        showInformation(viewHolder);
        return convertView;
    }

    //获取控件实例
    private void getInstance(ViewHolder viewHolder, View view) {
        viewHolder.washpattern=(TextView)view.findViewById(R.id.textView_pattern_washpatternlistviewitem);
        viewHolder.price=(TextView)view.findViewById(R.id.textView_price_washpatternlistviewitem);
        viewHolder.time=(TextView)view.findViewById(R.id.textView_time_washpatternlistviewitem);

    }
    //显示控件信息
    @SuppressLint("ResourceAsColor")
    private void showInformation(ViewHolder viewHolder) {
        viewHolder.washpattern.setText(pattern.getType());
        if(pattern.getPattern()==1)
            viewHolder.washpattern.setCompoundDrawablesWithIntrinsicBounds(getContext().getResources().getDrawable(R.drawable.normalwashing),null,null,null);
        else
            viewHolder.washpattern.setCompoundDrawablesWithIntrinsicBounds(getContext().getResources().getDrawable(R.drawable.quickwashing),null,null,null);
        viewHolder.price.setText(String.valueOf(pattern.getPrice())+"￥");
        viewHolder.time.setText(pattern.getInterval()+"分钟");
    }
}
